package cn.ad.service;

import cn.ad.entity.BaseReportValue;
import cn.ad.handler.SqlSessionHandler;
import cn.ad.model.IBaseRptModel;
import cn.ad.model.MediaRptResult;
import cn.ad.model.Search;
import cn.ad.model.SpaceSearch;
import cn.ad.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by zooy on 16-12-28.
 */
@Service
public class AdSpaceReportService {

    @Autowired
    private SqlSessionHandler sqlSessionHandler;

    /**
     * 指定订单下的所有广告位的数据报告列表
     */
    public BaseReportValue getCampaignAdSpaceListReport(Integer campaignId,Integer spaceId, Date startTime, Date endTime) {
        Utils.isCurrentTime(startTime, endTime);
        SpaceSearch search = new SpaceSearch();
        search.setCampaignId(campaignId);
        search.setEndTime(endTime);
        search.setStartTime(startTime);
        search.setSid(spaceId);
        Utils.resizeSearch(search, startTime, endTime);
        return sqlSessionHandler.selectOne("rptAdSpaceMapper.rpt:campaign:adspace:sum", search);
    }

    /**
     * 根据广告位ID获取对应两月内的投放数据的数量
     * @param adId
     * @return
     */
    public int getAdSpaceRptDataCount(int adId) {
        return sqlSessionHandler.selectOne("rptAdSpaceMapper.checkAdSpaceHasData",adId);
    }

    /**
     * 媒体广告位列表投放数据报告
     * @param spaceId 广告位ID
     * @param startTime 查询条件开始日期
     * @param endTime 查询条件结束日期
     * @return
     */
    public MediaRptResult getAdSpaceMediaRpt(Integer spaceId, Date startTime, Date endTime) {
        Date currentDateStartTime = Utils.getStartTime(Calendar.getInstance().getTime());
        if (startTime==null){
            startTime=currentDateStartTime;
        }
        if(currentDateStartTime.getTime()<startTime.getTime()){
            return null;
        }
        SpaceSearch search=new SpaceSearch();
        search.setSid(spaceId);
        search.setStartTime(startTime);
        search.setEndTime(endTime);
        Utils.resizeSearch(search, startTime, endTime);
        return sqlSessionHandler.selectOne("rptAdSpaceMapper.adSpace:rpt:list:sum",search);
    }

    public List<IBaseRptModel> getCampaignSpaceListReport(Integer campaignId, Date startTime, Date endTime) {
        Date currentDateStartTime = Utils.getStartTime(Calendar.getInstance().getTime());
        if (startTime==null){
            startTime=currentDateStartTime;
        }
        if(currentDateStartTime.getTime()<startTime.getTime()){
            return null;
        }
        Search search=new Search();
        search.setCampaignId(campaignId);
        search.setStartTime(startTime);
        search.setEndTime(endTime);
        Utils.resizeSearch(search, startTime, endTime);
        return sqlSessionHandler.selectList("rptAdSpaceMapper.adSpace:campaign:rpt:list:sum", search);
    }
}
